angular.module("app").controller("payCtrl", ['$scope', 'userInfo', '$http', '$state', 'utils', function($scope, userInfo, $http, $state, utils) {

	/*获取绑卡信息*/
	if($state.current.name == "PayOne") {
		userInfo.getUserBank().then(function(res) {
			$scope.userBankInfo = res.body.card
			userInfo.acc_id = res.body.card.acc_id
			userInfo.acc_number = res.body.card.acc_number
		})
	}

	/*获取用户银行卡预留手机号*/
	if($state.current.name == "PayTwoCode") {
		$http.get(utils.paySys, {
			params: {
				header: utils.header("020028"),
				body: {
					user_code: userInfo.user_code
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$scope.pre_phone = res.body.pre_phone
			}
		})
	}

	/*赋值预留手机号*/
	$scope.amount = userInfo.payamount
	$scope.pwd = "" //交易密码
	$scope.code = "" //验证码

	/*创建订单*/
	$scope.payone = function() {

		if(Number($scope.amount) < Number($scope.userBankInfo.min_amount)) {
			utils.msg("低于单笔最低限额")
			return
		}

		if(Number($scope.amount) > Number($scope.userBankInfo.max_amount * 10000)) {
			utils.msg("超出银行卡支付限额，建议在官网进行充值")
			return
		}

		$http.get(utils.OrderSys, {
				params: {
					header: utils.header("010010"),
					body: {
						acc_id: userInfo.acc_id,
						acc_number: userInfo.acc_number,
						amount: $scope.amount,
						order_source: "0",
						order_type: "1",
						pro_id: "",
						pro_num: "",
						pro_type: "",
						red_packet_list: "",
						user_code: userInfo.user_code
					}
				}
			})

			.then(function(res) {
				if(res.header.re_code == "0000") {
					userInfo.order_id = res.body.order_id
					/*充值申请*/
					$http.get(utils.tradeSys, {
							params: {
								header: utils.header("000010"),
								body: {
									order_id: userInfo.order_id,
									user_code: userInfo.user_code
								}
							}
						})

						.then(function(res) {
							if(res.header.re_code == "0000") {
								/*判断跳转页面*/
								$http.get(utils.paySys, {
										params: {
											header: utils.header("020025"),
											body: {
												user_code: userInfo.user_code
											}
										}
									})

									.then(function(res) {
										if(res.header.re_code == "0000") {
											userInfo.payamount = $scope.amount
											userInfo.pay_platform_type = res.body.status
											if(res.body.status == "0") {
												$state.go("PayTwoCode")
											} else {
												$state.go("PayTwoPwd")
											}
										}
									})
							}

						})
				}

			})
	}

	$scope.paytwo = function() {
		$http.get(utils.tradeSys, {
			params: {
				header: utils.header("000011"),
				body: {
					order_id: userInfo.order_id, //订单id
					order_type: "",
					pay_platform_type: userInfo.pay_platform_type, //支付平台类型
					tra_password: md5($scope.pwd),
					user_code: userInfo.user_code,
					vercd: $scope.code //验证码
				}
			}
		}).then(function(res) {
			if(res.header.re_code == "0000") {
				$state.go("PayThree")
			}
		})
	}

	/*订单详情*/
	if($state.current.name == "PayThree") {
		$http.get(utils.OrderSys, {
			params: {
				header: utils.header("010060"),
				body: {
					order_id: userInfo.order_id
				}
			}
		}).then(function(res) {

			if(res.header.re_code == "0000") {
				$scope.orderInfo = res.body
			}

		})

	}

}])